
package com.joysuch.wwyt.mh.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.Splitter;
import com.joysuch.wwyt.bp.enums.DataStateTypes;
import com.joysuch.wwyt.bp.repository.BpDeviceFacilityCollectionDao;
import com.joysuch.wwyt.common.constant.Constant;
import com.joysuch.wwyt.core.bean.BaseAttachmentBean;
import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.bean.SimpleUserBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.core.common.util.SimpleDateFormatCache;
import com.joysuch.wwyt.core.entity.BaseUser;
import com.joysuch.wwyt.core.enums.Status;
import com.joysuch.wwyt.core.exception.ApiBusinessException;
import com.joysuch.wwyt.core.repository.BaseUserDao;
import com.joysuch.wwyt.core.service.BizCodeGeneratorFacade;
import com.joysuch.wwyt.dataperm.annotation.EnableDataPermission;
import com.joysuch.wwyt.mh.BizUtils;
import com.joysuch.wwyt.mh.bean.MhMajorHazardChemProductRelAddBean;
import com.joysuch.wwyt.mh.bean.MhMajorHazardChemProductRelListBean;
import com.joysuch.wwyt.mh.bean.MhMajorHazardListBean;
import com.joysuch.wwyt.mh.bean.vo.MajorHazardDeleteCheckVO;
import com.joysuch.wwyt.mh.entity.MhMajorHazard;
import com.joysuch.wwyt.mh.entity.MhMajorHazardChemProduct;
import com.joysuch.wwyt.mh.entity.MhMajorHazardChemProductRel;
import com.joysuch.wwyt.mh.entity.MhMajorHazardPlan;
import com.joysuch.wwyt.mh.entity.dsl.QMhMajorHazard;
import com.joysuch.wwyt.mh.entity.dsl.QMhMajorHazardChemProduct;
import com.joysuch.wwyt.mh.entity.dsl.QMhMajorHazardChemProductRel;
import com.joysuch.wwyt.mh.enums.MajorHazardLevelEnum;
import com.joysuch.wwyt.mh.mapper.MhMajorHazardMapper;
import com.joysuch.wwyt.mh.repository.MhMajorHazardChemProductDao;
import com.joysuch.wwyt.mh.repository.MhMajorHazardChemProductRelDao;
import com.joysuch.wwyt.mh.repository.MhMajorHazardDao;
import com.joysuch.wwyt.mh.service.MhMajorHazardPlanService;
import com.joysuch.wwyt.mh.service.MhMajorHazardService;
import com.joysuch.wwyt.risk.entity.RiskDistrict;
import com.joysuch.wwyt.risk.repository.RiskDistrictDao;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.transaction.Transactional;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
@Transactional
public class MhMajorHazardServiceImpl implements MhMajorHazardService {

    @Autowired
    private MhMajorHazardDao mhMajorHazardDao;
    @Autowired
    private MhMajorHazardChemProductRelDao mhMajorHazardChemProductRelDao;
    @Autowired
    private MhMajorHazardChemProductDao mhMajorHazardChemProductDao;
    @Autowired
    private BizCodeGeneratorFacade bizCodeGeneratorFacade;
    @Autowired
    private RiskDistrictDao riskDistrictDao;
    @Autowired
    private BaseUserDao baseUserDao;
    @Autowired
    private BpDeviceFacilityCollectionDao bpDeviceFacilityCollectionDao;
    @Autowired
    private MhMajorHazardTaskAssignServiceImpl mhMajorHazardTaskAssignService;
    @Autowired
    private MhMajorHazardPlanService mhMajorHazardPlanService;
    @Autowired
    private MhMajorHazardMapper mhMajorHazardMapper;

    @Override
    public Page<MhMajorHazardListBean> list(Pageable pageable, String name, String levels) {
        pageable = PageDataUtils.addOrderByDesc(pageable, "id");
        QMhMajorHazard mhMajorHazard = QMhMajorHazard.mhMajorHazard;
        Predicate predicate = mhMajorHazard.isNotNull();
        predicate = name == null ? predicate
                : ExpressionUtils.and(predicate, ExpressionUtils.or(mhMajorHazard.name.like("%" + name + "%"),
                mhMajorHazard.code.like("%" + name + "%")));
        predicate = ExpressionUtils.and(predicate, ExpressionUtils.or(mhMajorHazard.state.notIn(DataStateTypes.DELETE.getState()),
                mhMajorHazard.state.isNull()));
        if (!StringUtils.isEmpty(levels)) {
            String[] level = levels.split(",");
            if (!ArrayUtil.isEmpty(level)){
                predicate = ExpressionUtils.and(predicate, mhMajorHazard.level.in(level));
            }
        }

        Page<MhMajorHazard> all = mhMajorHazardDao.findAll(predicate, pageable);
        List<MhMajorHazardListBean> list = new ArrayList<>();
        if (!all.isEmpty()) {
            for (MhMajorHazard m : all) {
                MhMajorHazardListBean mhMajorHazardListBean = new MhMajorHazardListBean();
                mhMajorHazardListBean.setId(m.getId());
                mhMajorHazardListBean.setCode(m.getCode());
                mhMajorHazardListBean.setLevel(m.getLevel());
                mhMajorHazardListBean.setLocation(m.getLocation());
                mhMajorHazardListBean.setName(m.getName());
                mhMajorHazardListBean.setRvalue(m.getRvalue());
                if (m.getRiskDistrictId() != null) {
                    Optional<RiskDistrict> ord = riskDistrictDao.findById(m.getRiskDistrictId());
                    if (ord.isPresent()) {
                        mhMajorHazardListBean.setLocation(ord.get().getName());
                    }
                }
                list.add(mhMajorHazardListBean);
                Set<MhMajorHazardChemProductRel> rels = m.getMhMajorHazardChemProductRels();
                String names = "";
                for (MhMajorHazardChemProductRel rel : rels) {
                    MhMajorHazardChemProduct product = rel.getMhMajorHazardChemProduct();
                    if (product != null) {
                        names += product.getName() + ",";
                    }
                }
                mhMajorHazardListBean.setChemProductName(names.length() == 0 ? names : names.substring(0, names.length() - 1));
            }

        }
        return new PageImpl<>(list, pageable, all.getTotalElements());
    }

    @Override
    public Page<MhMajorHazard> page(Pageable pageable, String name, Integer status) {
        pageable = PageDataUtils.addOrderByDesc(pageable, "id");
        QMhMajorHazard mhMajorHazard = QMhMajorHazard.mhMajorHazard;
        Predicate predicate = mhMajorHazard.isNotNull();
        predicate = ExpressionUtils.and(predicate, ExpressionUtils.or(mhMajorHazard.state.notIn(DataStateTypes.DELETE.getState()),
                mhMajorHazard.state.isNull()));
        if (Integer.valueOf(0).equals(status)) {
            predicate = ExpressionUtils.and(predicate, mhMajorHazard.location.isNull());
        } else if (Integer.valueOf(1).equals(status)) {
            predicate = ExpressionUtils.and(predicate, mhMajorHazard.location.isNotNull());
        }
        predicate = name == null ? predicate
                : ExpressionUtils.and(predicate, mhMajorHazard.name.like("%" + name + "%"));
        return mhMajorHazardDao.findAll(predicate, pageable);
    }

    @Override
    public List<MhMajorHazard> findAllHaveLocationForTank() {
        return mhMajorHazardDao.findAllHaveLocationForTank();
    }

    @Override
    public Long getNumForHaveLocation(Integer flag) {
        QMhMajorHazard mhMajorHazard = QMhMajorHazard.mhMajorHazard;
        Predicate predicate = mhMajorHazard.isNotNull();
        predicate = ExpressionUtils.and(predicate, ExpressionUtils.or(mhMajorHazard.state.notIn(DataStateTypes.DELETE.getState()),
                mhMajorHazard.state.isNull()));
        if (Integer.valueOf(1).equals(flag)) {
            predicate = ExpressionUtils.and(predicate, mhMajorHazard.location.isNotNull());
        }
        return mhMajorHazardDao.count(predicate);
    }

    @Override
    public MhMajorHazardChemProduct gethcs(Long id) {
        QMhMajorHazardChemProduct mhMajorHazardChemProduct = QMhMajorHazardChemProduct.mhMajorHazardChemProduct;
        Predicate predicate = mhMajorHazardChemProduct.isNotNull();
        predicate = ExpressionUtils.and(predicate, ExpressionUtils.or(mhMajorHazardChemProduct.state.notIn(DataStateTypes.DELETE.getState()),
                mhMajorHazardChemProduct.state.isNull()));
        predicate = id == null ? predicate : ExpressionUtils.and(predicate, mhMajorHazardChemProduct.id.eq(id));
        Iterable<MhMajorHazardChemProduct> all = mhMajorHazardChemProductDao.findAll(predicate);
        for (MhMajorHazardChemProduct majorHazardChemProduct : all) {
            return majorHazardChemProduct;
        }
        return null;
    }

    @Override
    public void add(MhMajorHazardChemProductRelListBean m) {
        List<MhMajorHazard> mList = mhMajorHazardDao.findByNameAndState(m.getName());
        if (!CollectionUtils.isEmpty(mList)) {
            throw new IllegalArgumentException("危险源名称已存在");
        }
        //Assert.isTrue(mhMajorHazardDao.countOfName(m.getName()) == 0, String.format("[%s]已存在", m.getName()));
        MhMajorHazard mhMajorHazard = new MhMajorHazard();
        BeanUtils.copyProperties(m, mhMajorHazard);
        if (StringUtils.isEmpty(m.getCode())) {
            mhMajorHazard.setCode(bizCodeGeneratorFacade.zhongDaWeiXianYuan());// 统一编码
        } else {
            int num = mhMajorHazardMapper.countByCodeAndState(m.getCode());
            if (num > 0) {
                throw new IllegalArgumentException("统一编码已存在");
            }
        }
        BaseAttachmentBean attachment = m.getAttachment();
        mhMajorHazard.setAttachment(attachment == null ? null : JSON.toJSONString(attachment));
        List<MhMajorHazardChemProductRelAddBean> mhMajorHazardChemProductRels = m.getMhMajorHazardChemProductRels();
        if (mhMajorHazardChemProductRels != null) {
            for (MhMajorHazardChemProductRelAddBean h : mhMajorHazardChemProductRels) {
                MhMajorHazardChemProductRel rel = new MhMajorHazardChemProductRel();
                rel.setMajorHazardId(m.getId());
                rel.setChemProductFeature(h.getChemProductFeature());// 化学危险性质
                rel.setUsage(h.getUsage());// 生产用途
                rel.setOperationPressure(h.getOperationPressure());/// 操作压力
                rel.setOperationTemperature(h.getOperationTemperature());// 操作温度
                rel.setDesignStock(h.getDesignStock());// 设计存量
                rel.setStock(h.getStock());// 存量
                rel.setCriticalStock(h.getCriticalStock());// 临界点
                rel.setSubstanceStatus(h.getSubstanceStatus());// 物理状态
                rel.setFixRate(h.getFixRate());// 校证系数
                if (h.getId() != null) {
                    MhMajorHazardChemProduct chem = mhMajorHazardChemProductDao.getOne(h.getId());
                    rel.setMhMajorHazardChemProduct(chem);
                }

                mhMajorHazard.getMhMajorHazardChemProductRels().add(rel);
                mhMajorHazardChemProductRelDao.save(rel);
            }
        }

        // mhMajorHazard.setState(DataStateTypes.EFFECTIVE.getState());
        mhMajorHazardDao.save(mhMajorHazard);
    }

    @Override
    public MhMajorHazardChemProductRelListBean getById(Long id) {
        MhMajorHazard m = mhMajorHazardDao.findById(id).get();
        MhMajorHazardChemProductRelListBean bean = new MhMajorHazardChemProductRelListBean();
        BeanUtils.copyProperties(m, bean);
        List<SimpleUserBean> responsibleUserIdList = new ArrayList<>();
        List<SimpleUserBean> operationUserIdList = new ArrayList<>();
        List<SimpleUserBean> technologyUserIdList = new ArrayList<>();
        if(!StringUtils.isEmpty(bean.getResponsibleUserId())){
            List<Long> responsibleUserIds = Arrays.asList(bean.getResponsibleUserId().split(",")).stream().map(e->Long.valueOf(e)).collect(Collectors.toList());
            responsibleUserIds.forEach(e ->{
                Optional<BaseUser> obu = baseUserDao.findById(e);
                    if (obu.isPresent()){
                        SimpleUserBean selectListBean = new SimpleUserBean();
                        selectListBean.setName(obu.get().getRealName());
                        selectListBean.setId(obu.get().getId());
                        responsibleUserIdList.add(selectListBean);
                    }
            });
        }
        bean.setResponsibleUserIdList(responsibleUserIdList);
        if(!StringUtils.isEmpty(bean.getTechnologyUserId())){
            List<Long> technologyUserIds = Arrays.asList(bean.getTechnologyUserId().split(",")).stream().map(e->Long.valueOf(e)).collect(Collectors.toList());
            technologyUserIds.forEach(e ->{
                Optional<BaseUser> obu = baseUserDao.findById(e);
                if (obu.isPresent()) {
                    SimpleUserBean selectListBean = new SimpleUserBean();
                    selectListBean.setName(obu.get().getRealName());
                    selectListBean.setId(obu.get().getId());
                    technologyUserIdList.add(selectListBean);
                }
            });
        }
        bean.setTechnologyUserIdList(technologyUserIdList);
        if(!StringUtils.isEmpty(bean.getOperationUserId())){
            List<Long> operationUserIds = Arrays.asList(bean.getOperationUserId().split(",")).stream().map(e->Long.valueOf(e)).collect(Collectors.toList());
            operationUserIds.forEach(e->{
                Optional<BaseUser> obu = baseUserDao.findById(e);
                if (obu.isPresent()) {
                    SimpleUserBean selectListBean = new SimpleUserBean();
                    selectListBean.setName(obu.get().getRealName());
                    selectListBean.setId(obu.get().getId());
                    operationUserIdList.add(selectListBean);
                }
            });
        }
        bean.setOperationUserIdList(operationUserIdList);
        Date date = m.getCreateTime();
        String attachment = m.getAttachment();
        bean.setAttachment(attachment == null ? null : JSONObject.parseObject(m.getAttachment(), BaseAttachmentBean.class));
        /* SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); */
        if (date != null) {
            bean.setCreateTime(SimpleDateFormatCache.getYmd().format(date));
        }
        if (m.getRiskDistrictId() != null) {
            RiskDistrict one = riskDistrictDao.getOne(m.getRiskDistrictId());
            bean.setRiskDistrictName(one.getName());// 所在区域
            bean.setRiskDistrictId(one.getId());// 所在区域
        }
        if (!StringUtils.isEmpty(m.getResponsibleUserId())) {
            String responsibleUserName;
            List<Long> userIds = Splitter.on(Constant.SPLIT_COMMA).trimResults().splitToList(m.getResponsibleUserId()).stream().map(Long::new).collect(Collectors.toList());
//            Optional<BaseUser> obu = baseUserDao.(m.getResponsibleUserId());
            if(CollectionUtil.isNotEmpty(userIds)) {
                responsibleUserName = baseUserDao.findRealNameByIdIn(userIds).stream().collect(Collectors.joining(Constant.SPLIT_COMMA));
                bean.setResponsibleUserName(responsibleUserName);
            }
        }
        if (!StringUtils.isEmpty(m.getOperationUserId())) {
            String operationUserName;
            List<Long> userIds = Splitter.on(Constant.SPLIT_COMMA).trimResults().splitToList(m.getOperationUserId()).stream().map(Long::new).collect(Collectors.toList());
            if(CollectionUtil.isNotEmpty(userIds)) {
                operationUserName = baseUserDao.findRealNameByIdIn(userIds).stream().collect(Collectors.joining(Constant.SPLIT_COMMA));
                bean.setOperationUserName(operationUserName);
            }
        }
        if (!StringUtils.isEmpty(m.getTechnologyUserId())) {
            String technologyUserName;
            List<Long> userIds = Splitter.on(Constant.SPLIT_COMMA).trimResults().splitToList(m.getTechnologyUserId()).stream().map(Long::new).collect(Collectors.toList());
            if(CollectionUtil.isNotEmpty(userIds)) {
                technologyUserName = baseUserDao.findRealNameByIdIn(userIds).stream().collect(Collectors.joining(Constant.SPLIT_COMMA));
                bean.setTechnologyUserName(technologyUserName);
            }
        }
        /*
         * if (m.getRiskDistrictId() != null) {
         * bean.setRiskDistrictName(riskDistrictDao.getOne(m.getRiskDistrictId()).getName());
         * }
         */
        Set<MhMajorHazardChemProductRel> mhMajorHazardChemProductRels = m.getMhMajorHazardChemProductRels();
        ArrayList<MhMajorHazardChemProductRelAddBean> ss = new ArrayList<>();
        for (MhMajorHazardChemProductRel mhMajorHazardChemProductRel : mhMajorHazardChemProductRels) {
            if (mhMajorHazardChemProductRel != null) {
                MhMajorHazardChemProductRelAddBean s = new MhMajorHazardChemProductRelAddBean();
                MhMajorHazardChemProductRel rel = mhMajorHazardChemProductRelDao
                        .getOne(mhMajorHazardChemProductRel.getId());
                // MhMajorHazardChemProductRel rel = mhMajorHazardChemProductRels.iterator().next();
                s.setRelId(rel.getId());
                s.setChemProductId(rel.getMhMajorHazardChemProduct().getId());
                s.setChemProductFeature(rel.getChemProductFeature());// 化学危险性质
                s.setOperationPressure(rel.getOperationPressure());/// 操作压力
                s.setOperationTemperature(rel.getOperationTemperature());// 操作温度
                s.setDesignStock(rel.getDesignStock());// 设计存量
                s.setStock(rel.getStock());// 存量
                s.setUsage(rel.getUsage());// 生产用途
                s.setCriticalStock(rel.getCriticalStock());// 临界点
                s.setSubstanceStatus(rel.getSubstanceStatus());// 物理状态
                s.setFixRate(rel.getFixRate());// 校证系数
                s.setName(rel.getMhMajorHazardChemProduct().getName());
                s.setCode(rel.getMhMajorHazardChemProduct().getCode());
                s.setId(rel.getMhMajorHazardChemProduct().getId());
                ss.add(s);
                bean.setHazardChemicalFlag(
                        BizUtils.translateYesNoToChinese(rel.getMhMajorHazardChemProduct().getHazardChemicalFlag()));// 是否事重大危险化学品
            }
        }
        bean.setMhMajorHazardChemProductRels(ss);
        return bean;
    }

    @Override
    public void edit(MhMajorHazardChemProductRelListBean m) {
        // 修改中单危险源属性
        MhMajorHazard entity = mhMajorHazardDao.getOne(m.getId());
        String oldCode = entity.getCode();
        BeanUtils.copyProperties(m, entity);
        if (StringUtils.isEmpty(m.getCode())) {
            entity.setCode(bizCodeGeneratorFacade.zhongDaWeiXianYuan());// 统一编码
        } else {
            int num = mhMajorHazardMapper.countByCodeAndState(m.getCode());
            if (!m.getCode().equals(oldCode) && num > 0) {
                throw new IllegalArgumentException("统一编码已存在");
            }
        }
        List<MhMajorHazardChemProductRelAddBean> mhMajorHazardChemProductRels = m.getMhMajorHazardChemProductRels();
        deleteRel(m.getId());
        if (mhMajorHazardChemProductRels != null) {
            for (MhMajorHazardChemProductRelAddBean h : mhMajorHazardChemProductRels) {
                MhMajorHazardChemProductRel rel = new MhMajorHazardChemProductRel();
                rel.setMajorHazardId(m.getId());
                rel.setChemProductFeature(h.getChemProductFeature());// 化学危险性质
                rel.setUsage(h.getUsage());// 生产用途
                rel.setOperationPressure(h.getOperationPressure());/// 操作压力
                rel.setOperationTemperature(h.getOperationTemperature());// 操作温度
                rel.setDesignStock(h.getDesignStock());// 设计存量
                rel.setStock(h.getStock());// 存量
                rel.setCriticalStock(h.getCriticalStock());// 临界点
                rel.setSubstanceStatus(h.getSubstanceStatus());// 物理状态
                rel.setFixRate(h.getFixRate());// 校证系数
                if (h.getId() != null) {
                    MhMajorHazardChemProduct chem = mhMajorHazardChemProductDao.getOne(h.getId());
                    rel.setMhMajorHazardChemProduct(chem);
                }
                entity.getMhMajorHazardChemProductRels().add(rel);
                mhMajorHazardChemProductRelDao.save(rel);
            }
        }
        BaseAttachmentBean attachment = m.getAttachment();
        entity.setAttachment(attachment == null ? null : JSON.toJSONString(attachment));
        mhMajorHazardDao.save(entity);
        List<MhMajorHazard> mList = mhMajorHazardDao.findByNameAndState(m.getName());
        if (mList.size() > 1) {
            throw new IllegalArgumentException("危险源名称已存在");
        }
    }

    @Override
    public void delete(Long id) throws ApiBusinessException {
        MhMajorHazard majorHazard = mhMajorHazardDao.findById(id).orElseThrow(() -> new IllegalArgumentException("危险源不存在"));
        MajorHazardDeleteCheckVO vo = checkMajorHazard(id);
        vo.setCode(majorHazard.getCode());
        vo.setName(majorHazard.getName());
        if (vo.getGuaranteeDutyNum() > 0) {
            throw new ApiBusinessException(Status.DELETE_CHECK_FAILED.getCode(), JSONObject.toJSONString(vo));
        }
        majorHazard.setState(DataStateTypes.DELETE.getState());
        //删除所有关联了危险源的设备表中的危险源id字段
        deleteMajorHazardAllBindings(id);
        mhMajorHazardDao.save(majorHazard);

        //包保责任制任务删除
        mhMajorHazardTaskAssignService.deleteByMhId(id);
    }

    private MajorHazardDeleteCheckVO checkMajorHazard(Long id) {
        // 重大危险源关联的计划数量
        int count = mhMajorHazardPlanService.count(new QueryWrapper<MhMajorHazardPlan>()
                .eq("major_hazard_id", id)
                .eq("delete_flag", "0"));
        MajorHazardDeleteCheckVO majorHazardDeleteCheckVO = new MajorHazardDeleteCheckVO();
        majorHazardDeleteCheckVO.setGuaranteeDutyNum(count);
        return majorHazardDeleteCheckVO;
    }

    @Override
    public Page<MhMajorHazardListBean> get(Pageable pageable) {
        return list(pageable, null, null);
    }

    @Override
    @EnableDataPermission
    public List<MhMajorHazard> findByResponsibleDept() {
        return mhMajorHazardDao.findAll();
    }

    /**
     * 获取危险源列表
     *
     * @return
     */
    @Override
    public List<SimpleSelectListBean> getList() {
        List<SimpleSelectListBean> list = new ArrayList<>();
        List<MhMajorHazard> all = mhMajorHazardDao.findAll();
        if (!CollectionUtils.isEmpty(all)) {
            for (MhMajorHazard mh : all) {
                SimpleSelectListBean bean = new SimpleSelectListBean();
                bean.setValue(mh.getId());
                bean.setLabel(mh.getName());
                list.add(bean);
            }
        }
        return list;
    }

    public void deleteRel(Long id) {
        QMhMajorHazardChemProductRel rel = QMhMajorHazardChemProductRel.mhMajorHazardChemProductRel;
        Predicate predicate = rel.isNotNull();
        predicate = id == null ? predicate : ExpressionUtils.and(predicate, rel.majorHazardId.eq(id));
        Iterable<MhMajorHazardChemProductRel> all = mhMajorHazardChemProductRelDao.findAll(predicate);
        for (MhMajorHazardChemProductRel mhMajorHazardChemProductRel : all) {
            mhMajorHazardChemProductRel.setDeleteFlag("1");
        }
    }

    /**
     * 删除所有绑定了危险源的设备设施等
     * @param MajorHazardId 危险源的编号
     */
    public void deleteMajorHazardAllBindings(Long MajorHazardId) {
        //删除生产装置的绑定
        mhMajorHazardDao.deleteProductionFacility(MajorHazardId);
        //删除储罐的绑定
        mhMajorHazardDao.deleteStorageTank(MajorHazardId);
        //删除储罐区的绑定
        mhMajorHazardDao.deleteStorageTankArea(MajorHazardId);
        //删除库房的绑定
        mhMajorHazardDao.deleteWarehouse(MajorHazardId);
        //删除危化品的绑定
        mhMajorHazardDao.deleteHazardousChemicals(MajorHazardId);
        //删除总设备表的绑定
//        mhMajorHazardChemProductRelDao.deleteByMajorHazardId(MajorHazardId);
        bpDeviceFacilityCollectionDao.deleteByMajorHazardId(MajorHazardId);
    }

    @Override
    public int getTaskNum(Long hazardId) {
        Integer taskNUm = mhMajorHazardDao.getTaskNum(hazardId);
        return taskNUm == null ? 0 : taskNUm;
    }

}
